﻿using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using ReadyApps.Data;
using ReadyApps.Data.Wiki;

namespace ReadyApps.Wiki.Views
{
    abstract class ContentViewBase : DataViewBase
    {
        #region Variables and Constants

        private static readonly Regex databaseSplitPattern = new Regex(@"(\{\[link:\d+\]\})", RegexOptions.Compiled);
        private static readonly Regex databaseLinkPattern = new Regex(@"\{\[link:(\d+)\]\}", RegexOptions.Compiled);

        private IWikiUrlManager urlManager;

        #endregion

        #region Constructor

        public ContentViewBase(int wikiPageID, WikiControlSettings wikiControlSettings,
            IWikiDataProvider wikiDataProvider, IWikiUrlManager wikiUrlManager)
            : base(wikiPageID, wikiControlSettings, wikiDataProvider)
        {
            if (wikiUrlManager == null)
            {
                throw new ArgumentNullException("wikiUrlManager");
            }
            urlManager = wikiUrlManager;
        }

        #endregion

        #region Shared Methods

        /// <summary>
        /// Retrieves the content and links for the current page and creates server controls from
        /// the parsed data source content. The optional <paramref name="editMode"/> parameter
        /// determines whether the internal wiki links should be marked with their page ID or not.
        /// </summary>
        /// <param name="content">The <see cref="PageContent"/> to build a control tree for.</param>
        /// <param name="hostControl">The control to add the generated controls to.</param>
        /// <param name="editMode">An optional parameter specifying if the controls should be generated
        /// in preparation for editing.</param>
        protected void GetContentControls(PageContent content, Control hostControl, bool editMode = false)
        {
            string[] pageContentPieces = databaseSplitPattern.Split(content.Text);
            Match match;
            foreach (var contentPiece in pageContentPieces)
            {
                // Avoid creating unnecessary controls
                if (!string.IsNullOrEmpty(contentPiece))
                {
                    match = databaseLinkPattern.Match(contentPiece);
                    if (match.Success)
                    {
                        int pageLinkKey = Convert.ToInt32(match.Groups[1].Value);
                        InternalPageLink pageLink = content.Links.Where(pl => pl.PageLinkKey == pageLinkKey).First();
                        HyperLink link = new HyperLink();
                        link.Text = pageLink.DisplayText;
                        if (editMode)
                        {
                            link.NavigateUrl = "javascript:void(0);";
                            link.Attributes.Add("pageid", pageLink.LinkPageID.ToString());
                        }
                        else
                        {
                            link.NavigateUrl = urlManager.GetVirtualUrl(pageLink.FullPath);
                        }
                        link.Target = pageLink.Target;
                        hostControl.Controls.Add(link);
                    }
                    else
                    {
                        hostControl.Controls.Add(new Literal() { Text = contentPiece });
                    }
                }
            }
        }

        #endregion
    }
}
